<?php
class AudienceModel extends Model{

	private $audience_id = "`audience_id`";
	private $audience_list_cows="a.`audience_id`,a.`audience_tel`,a.`audience_email`,a.`audience_name`,a.`audience_position`,a.`audience_portrait`,b.`department_name` as `audience_department`,a.`audience_imUsername` ,a.`audience_imPassword`";

	//获取用户列表
	public function get_audience_list($data){
		$sql = "select ".$this->audience_list_cows." from audience as a";
		$sql.= " join audience_department as b on a.audience_department=b.department_id";
		$sql.= " where audience_state!=0";
		$sql.= " order by a.audience_nick";
		if($_REQUEST['startid']!=null)
			$sql.=" and ".$this->audience_id." >=".$data['startid'];
		else
			$sql.= " limit ".(($data['page']-1)*$data['count']).",".$data['count'];
		return M("audience","",'DB_MEETING')->query($sql);
	}

	//获取用户信息
	public function get_audience_name_by_uid($uid){
		return M('Audience','','DB_MEETING')->where('audience_id='.$uid)->getField('audience_name');
	}
	/*
	 *get_audience 查询当前用户数据
	 *arg
	 *uid int
	 *usernmae varchar
	 *****/
	public function get_audience($uid='',$username=''){
		$sql = "select * from audience as a";
		$sql.= " join audience_department as b on a.audience_department=b.department_id";
		if($uid!='')
			$sql.= ' where a.audience_id='.$uid;
		else
			$sql.= ' where a.audience_username="'.$username.'"';
		$rs= M('Audience','','DB_MEETING')->limit(1)->query($sql);

		$rs[0]['is_leader']=M('audience_department','','DB_MEETING')->field('department_id')->where('leader_id='.$rs[0]['audience_id'])->find()?true:false;
		$rs[0]['is_personnel']=M('department_personnel','','DB_MEETING')->field('dpid')->where('personnel_id='.$rs[0]['audience_id'])->find()?true:false;

		return $rs[0];
	}
	/*
	 *find_pwd 查询当前用户密码
	 *arg
	 *tel varchar
	 *email varchar
	 *****/
	public function find_pwd($tel,$email){
		if($tel!=''){
			return M('Audience','','DB_MEETING')->where('audience_tel="'.$tel.'"')->getField('audience_pwd');
		}else{
			return M('Audience','','DB_MEETING')->where('audience_email="'.$email.'"')->getField('audience_pwd');
		}
	}
	/*
	 *find_person
	 *通过姓名或者职位查找人或项目
	 *arg
	 *conditions varchar
	 ****/
	public function find_person($conditions){
		$sql = 'select '.$this->audience_list_cows.' from audience as a';
		$sql.= " join audience_department as b on a.audience_department=b.department_id";
		$sql.= " where audience_state!=0";
		$sql.= ' and (audience_name like "'.$conditions.'%"';//通过姓名
		$sql.= ' or audience_position like "'.$conditions.'%"';//通过职位
		$sql.= ' or b.department_name like "'.$conditions.'%"';//通过部门
		$sql.= ')';
		return M('Audience','','DB_MEETING')->limit(30)->query($sql);
	}

	public function update_audience($data){
		return M('Audience','','DB_MEETING')->where('audience_id='.$data['uid'])->save($data);
	}

	public function get_audience_by_imUsernameArray($imUsernameArray){
		$sql='';
		foreach($imUsernameArray as $val){
			$sqlnew = 'select '.$this->audience_list_cows.' from audience as a';
			$sqlnew.= ' join audience_department as b on a.audience_department=b.department_id';
			$sqlnew.= ' where a.audience_imUsername="'.$val.'"';
			if($sql==''){
				$sql=$sqlnew;
			}else{
				$sql.=' union '.$sqlnew;
			}
		}
		return M("audience","",'DB_MEETING')->query($sql);
	}

	public function get_leader_id($uid){
		$sql ="select b.leader_id from audience as a";
		$sql.=" join audience_department as b on a.audience_department=b.department_id";
		$sql.=" where a.audience_id={$uid}";
		$sql.=" limit 1";
		$rs = M('audience','','DB_MEETING')->query($sql);
		return $rs[0]['leader_id'];
	}

	public function get_user_meeting_group($uid)
	{
		$group = 0;
		$rs = M('audience_meeting_category','','DB_MEETING')->where('audience_id=%d',$uid)->select();
		foreach($rs as $val)
		{
			$group = ','.$val['category_id'];
		}
	}


}
?>